﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading;

namespace MyEmulators2
{
    internal delegate void SearchCompletedHandler(RomMatch romMatch);
    internal delegate void ThumbDownloadedHandler(Bitmap image, bool isCovers);
    internal delegate void ThumbDownloadCompleteHandler();

    class ThumbRetriever
    {
        Importer importer = null;

        Thread searchThread = null;
        Thread thumbThread1 = null;
        Thread thumbThread2 = null;

        public event ThumbDownloadedHandler OnThumbDownloaded;
        public event SearchCompletedHandler OnSearchCompleted;
        public event ThumbDownloadCompleteHandler OnDownloadComplete;

        public ThumbRetriever()
        {
            importer = new Importer();
            importer.DoWork = true;
        }

        public void Search(string title, Game game)
        {
            RomMatch romMatch = new RomMatch(game);
            romMatch.Title = title; //don't alter game's title
            importer.ScraperProvider.Update();
            searchThread = new Thread(new ThreadStart(delegate()
                {
                    try
                    {
                        importer.ScraperProvider.GetMatches(romMatch);
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex);
                    }
                    finally
                    {
                        if (OnSearchCompleted != null)
                            OnSearchCompleted(romMatch);
                    }
                }
                )) { Name = "ThumbSearchThread" };
            searchThread.Start();
        }

        //This is written a bit weird, we initialise both threads before starting
        //then first thread retrieves list of thumb url's, then starts second thread dloading screens,
        //then starts dloading covers itself 
        public void RetrieveThumbs(RomMatch romMatch)
        {
            if (romMatch.GameDetails == null)
                return;

            string[] covers = null;
            string[] screens = null;

            //initialise first thread
            thumbThread1 = new Thread(new ThreadStart(delegate()
            {
                try
                {
                    //get thumb url's
                    romMatch.GameDetails.DataProvider.GetThumbs(romMatch, out covers, out screens);

                    if (!importer.DoWork)
                        return;

                    //start second thread retrieving screens
                    thumbThread2.Start();
                    //then retrieve covers
                    downloadThumbs(covers, true);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }
                finally
                {
                    if (thumbThread2 != null && thumbThread2.IsAlive)
                        thumbThread2.Join();
                    if (OnDownloadComplete != null)
                        OnDownloadComplete();
                }
            }
            )) { Name = "ThumbDownloadThread1" };

            //initialise second thread
            thumbThread2 = new Thread(new ThreadStart(delegate()
            {
                try
                {
                    downloadThumbs(screens, false);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }
            }
            )) { Name = "ThumbDownloadThread2" };

            //start dload
            thumbThread1.Start();
        }

        public void Stop(bool wait)
        {
            importer.DoWork = false;
            if (wait)
            {
                if (searchThread != null && searchThread.IsAlive)
                    searchThread.Join();
                if (thumbThread1 != null && thumbThread1.IsAlive)
                    thumbThread1.Join();
                if (thumbThread2 != null && thumbThread2.IsAlive)
                    thumbThread2.Join();
                importer.DoWork = true;
            }
        }

        void downloadThumbs(string[] thumbs, bool isCovers)
        {
            if (thumbs == null)
                return;

            foreach (string thumb in thumbs)
            {
                if (!importer.DoWork)
                    return;

                Bitmap image = ImageHandler.Instance.BitmapFromWeb(thumb);
                if (image != null && OnThumbDownloaded != null)
                {
                    OnThumbDownloaded(image, isCovers);
                }
            }
        }
    }
}
